package org.tangdao.modules.file.model.vo;

import java.io.Serializable;
import java.util.Map;

import org.tangdao.common.collect.MapUtils;
import org.tangdao.common.config.Global;
import org.tangdao.common.io.FileUtils;
import org.tangdao.common.lang.DateUtils;
import org.tangdao.common.lang.StringUtils;

import lombok.Data;

/**
 * 上传文件入参
 * 
 * @author Ryan Ru(ruyangit@gmail.com)
 */
@Data
public class FileUploadParams implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	public static final String FILEUPLOAD_BASE_URL = "fileupload";

	private String uploadType;
	private String uploadPath;
	private String relativePath;
	private String bizKey;
	private String bizType;
	private String allowSuffixes;
	private String mediaAllowSuffixes;
	private String imageAllowSuffixes;
	private Integer imageMaxWidth;
	private Integer imageMaxHeight;
	private Long maxFileSize;
	private String fileMd5;
	private String fileName;
	private String fileExtension;
	private String fileAllowSuffixes;
	private Map<String, String> extend;

	public FileUploadParams() {
		this.uploadType = "file";
		this.imageMaxWidth = -1;
		this.imageMaxHeight = -1;
		this.extend = MapUtils.newHashMap();
	}

	public FileUploadParams initialize() {
		this.uploadPath = Global.getProperty("file.uploadPath", "{yyyy}{MM}/");
		String[] dds = StringUtils.substringsBetween(this.uploadPath, "{", "}");
		if (dds != null) {
			for (String dd : dds) {
				if (StringUtils.inString(dd, "yyyy", "MM", "dd", "HH", "mm", "ss", "E")) {
					this.uploadPath = StringUtils.replace(this.uploadPath, "{" + dd + "}", DateUtils.getDate(dd));
				}
			}
		}
		this.relativePath = FileUtils.path(this.uploadPath);

		String maxFileSize = Global.getProperty("file.maxFileSize");

		if (StringUtils.isNotBlank(maxFileSize)) {
			String[] maxFileSizes = StringUtils.split(maxFileSize, "*");
			this.maxFileSize = 1L;
			for (String mfs : maxFileSizes) {
				this.maxFileSize = Long.valueOf(mfs) * this.maxFileSize;
			}
		} else {
			this.maxFileSize = 0L;
		}

		if (this.maxFileSize < 1L) {
			this.maxFileSize = 5242880L;
		}

		if (this.fileName != null) {
			this.fileName = this.fileName.trim();
			this.fileExtension = FileUtils.getFileExtension(this.fileName);
		}

		this.imageAllowSuffixes = Global.getProperty("file.imageAllowSuffixes", "-1");
		this.mediaAllowSuffixes = Global.getProperty("file.mediaAllowSuffixes", "-1");
		this.fileAllowSuffixes = Global.getProperty("file.fileAllowSuffixes", "-1");
		if ("image".equals(this.uploadType)) {
			this.allowSuffixes = this.imageAllowSuffixes;
			return this;
		} else if ("media".equals(this.uploadType)) {
			this.allowSuffixes = this.mediaAllowSuffixes;
			return this;
		} else if ("file".equals(this.uploadType)) {
			this.allowSuffixes = this.fileAllowSuffixes;
			return this;
		} else {
			if (StringUtils.contains(this.imageAllowSuffixes, "." + this.fileExtension + ",")) {
				this.uploadType = "image";
			}

			if (StringUtils.contains(this.mediaAllowSuffixes, "." + this.fileExtension + ",")) {
				this.uploadType = "media";
			}

			if (StringUtils.contains(this.fileAllowSuffixes, "." + this.fileExtension + ",")) {
				this.uploadType = "file";
			}

			this.allowSuffixes = this.imageAllowSuffixes + "," + this.mediaAllowSuffixes + "," + this.fileAllowSuffixes;
			return this;
		}

	}

}
